package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 项目日志类型
 * @author zheng.li
 * @version v1.0
 * @date 2019/06/13 19:54
 * @since v1.0
 */
@Getter
public enum ProjectLogEnum {
    /**
     * 客户
     */
    CUSTOMER_CREATE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1001, "enum.projectLog.name.customer_create"),
    CUSTOMER_HANDOVER(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1002, "enum.projectLog.name.customer_handover"),
    CUSTOMER_GRAB(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1003, "enum.projectLog.name.customer_grab"),
    CUSTOMER_DISTRIBUTE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1004, "enum.projectLog.name.customer_distribute"),
    CUSTOMER_STAGE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1005, "enum.projectLog.name.customer_stage"),
    CUSTOMER_BACK(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1006, "enum.projectLog.name.customer_back"),
    CUSTOMER_MERGE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1007, "enum.projectLog.name.customer_merge"),
    CUSTOMER_UPDATE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1008, "enum.projectLog.name.customer_update"),
    CUSTOMER_DELETE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1009, "enum.projectlog.name.customer_delete"),
    CUSTOMER_RESTORE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1010, "enum.projectLog.name.customer_restore"),
    CUSTOMER_ARCHIVE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1011, "enum.projectLog.name.customer_archive"),
    CUSTOMER_REF(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1012, "enum.projectLog.name.customer_ref"),
    // 客户团队添加
    CUSTOMER_TEAM_ADD(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1013, "enum.projectLog.name.customer_team_add"),
    CUSTOMER_TEAM_DELETE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1014, "enum.projectLog.name.customer_team_delete"),
    CUSTOMER_TEAM_UPDATE(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1015, "enum.projectLog.name.customer_team_update"),
    CUSTOMER_TEAM_HANDOVER(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1016, "enum.projectLog.name.customer_team_handover"),
    CUSTOMER_SMS(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1017, "enum.projectLog.name.sms_send"),
    CUSTOMER_EXIT_TEAM(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), 1018, "enum.projectLog.name.exit_team"),

    /**
     * 机会
     */
    OPPORTUNITY_CREATE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3011, "enum.projectLog.name.opportunity_create"),
    OPPORTUNITY_STAGE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3012, "enum.projectLog.name.opportunity_stage"),
    OPPORTUNITY_REF(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3013, "enum.projectLog.name.opportunity_ref"),
    OPPORTUNITY_UPDATE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3014, "enum.projectLog.name.opportunity_update"),
    OPPORTUNITY_DELETE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3015, "enum.projectLog.name.opportunity_delete"),
    OPPORTUNITY_ARCHIVE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3016, "enum.projectLog.name.opportunity_archive"),
    OPPORTUNITY_TEAM_ADD(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3017, "enum.projectLog.name.opportunity_team_add"),
    OPPORTUNITY_TEAM_DELETE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3018, "enum.projectLog.name.opportunity_team_delete"),
    OPPORTUNITY_TEAM_UPDATE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3019, "enum.projectLog.name.opportunity_team_update"),
    OPPORTUNITY_TEAM_HANDOVER(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3020, "enum.projectLog.name.opportunity_team_handover"),
    OPPORTUNITY_RESTORE(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), 3021, "enum.projectLog.name.opportunity_restore"),

    /**
     * 合同
     */
    CONTRACT_CREATE(XbbRefTypeEnum.CONTRACT.getCode(), 2011, "enum.projectLog.name.contract_create"),
    CONTRACT_QUOTATION(XbbRefTypeEnum.CONTRACT.getCode(), 2012, "enum.projectLog.name.contract_quotation"),
    CONTRACT_UPDATE(XbbRefTypeEnum.CONTRACT.getCode(), 2013, "enum.projectLog.name.contract_update"),
    CONTRACT_DELETE(XbbRefTypeEnum.CONTRACT.getCode(), 2014, "enum.projectLog.name.contract_delete"),
    CONTRACT_ARCHIVE(XbbRefTypeEnum.CONTRACT.getCode(), 2015, "enum.projectLog.name.contract_archive"),
    CONTRACT_REF(XbbRefTypeEnum.CONTRACT.getCode(), 2016, "enum.projectLog.name.contract_ref"),
    CONTRACT_STAGE(XbbRefTypeEnum.CONTRACT.getCode(), 2017, "enum.projectLog.name.contract_stage"),
    CONTRACT_TEAM_ADD(XbbRefTypeEnum.CONTRACT.getCode(), 2018, "enum.projectLog.name.contract_team_add"),
    CONTRACT_TEAM_DELETE(XbbRefTypeEnum.CONTRACT.getCode(), 2019, "enum.projectLog.name.contract_team_delete"),
    CONTRACT_TEAM_UPDATE(XbbRefTypeEnum.CONTRACT.getCode(), 2020, "enum.projectLog.name.contract_team_update"),
    CONTRACT_TEAM_HANDOVER(XbbRefTypeEnum.CONTRACT.getCode(), 2021, "enum.projectLog.name.contract_team_handover"),
    CONTRACT_RESTORE(XbbRefTypeEnum.CONTRACT.getCode(), 2022, "enum.projectLog.name.contract_restore"),

    PAYMENT_CREATE(XbbRefTypeEnum.PAYMENT.getCode(), 7011, "enum.projectLog.name.payment_create"),
    PAYMENT_BAD(XbbRefTypeEnum.PAYMENT.getCode(), 7041, "enum.projectLog.name.payment_bad"),
    PAYMENT_DELETE(XbbRefTypeEnum.PAYMENT.getCode(), 7042, "enum.projectLog.name.payment_delete"),
    /**
     * 退货退款
     */
    REFUND_CREATE(XbbRefTypeEnum.REFUND.getCode(), 2021, "enum.projectLog.name.refund_create"),
    REFUND_DETELE(XbbRefTypeEnum.REFUND.getCode(), 2022, "enum.projectLog.name.refund_delete"),

    /**
     * 订货单
     */
    ORDER_CREATE(XbbRefTypeEnum.ORDER.getCode(), 95101, "enum.projectLog.name.order_create"),
    ORDER_DELETE(XbbRefTypeEnum.ORDER.getCode(), 95102, "enum.projectLog.name.order_delete"),

    /**
     * 通话记录
     */
    CALL_LOG(XbbRefTypeEnum.CALL_CENTER.getCode(), 36011, "enum.projectLog.name.call_log"),
    /**
     * 报价单
     */
    QUOTATION_CREATE(XbbRefTypeEnum.QUOTATION.getCode(), 46001, "enum.projectLog.name.quotation_create"),
    QUOTATION_NEW_VERSION(XbbRefTypeEnum.QUOTATION.getCode(), 46002, "enum.projectLog.name.quotation_new_version"),
    QUOTATION_REF(XbbRefTypeEnum.QUOTATION.getCode(), 46003, "enum.projectLog.name.quotation_ref"),
    QUOTATION_DELETE(XbbRefTypeEnum.QUOTATION.getCode(), 46004, "enum.projectLog.name.quotation_delete"),
    QUOTATION_UPDATE(XbbRefTypeEnum.QUOTATION.getCode(), 46005, "enum.projectLog.name.quotation_update"),
    QUOTATION_ARCHIVE(XbbRefTypeEnum.QUOTATION.getCode(), 46006, "enum.projectLog.name.quotation_archive"),

    /**
     * 经销商
     */
    DISTRIBUTOR_CREATE(XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(), 56001, "enum.projectLog.name.distributor_create"),
    DISTRIBUTOR_HANDOVER(XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(), 56002, "enum.projectLog.name.distributor_handover"),
    DISTRIBUTOR_CALL_LOG(XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(), 56003, "enum.projectLog.name.call_log"),
    /**
     * 线索
     */
    CLUE_CREATE(XbbRefTypeEnum.CLUE.getCode(), 47001, "enum.projectLog.name.clue_create"),
    CLUE_HANDOVER(XbbRefTypeEnum.CLUE.getCode(), 47002, "enum.projectLog.name.clue_handover"),
    CLUE_GRAB(XbbRefTypeEnum.CLUE.getCode(), 47003, "enum.projectLog.name.clue_grab"),
    CLUE_DISTRIBUTE(XbbRefTypeEnum.CLUE.getCode(), 47004, "enum.projectLog.name.clue_distribute"),
    CLUE_STAGE(XbbRefTypeEnum.CLUE.getCode(), 47005, "enum.projectLog.name.clue_stage"),
    CLUE_BACK(XbbRefTypeEnum.CLUE.getCode(), 47006, "enum.projectLog.name.clue_back"),

    /**
     * 联系人
     */
    CONTACT_UPDATE(XbbRefTypeEnum.CONTACT.getCode(), 60001, "enum.projectLog.name.contact_update"),
    CONTACT_REF(XbbRefTypeEnum.CONTACT.getCode(), 60002, "enum.projectLog.name.contact_ref"),
    CONTACT_DELETE(XbbRefTypeEnum.CONTACT.getCode(), 60003, "enum.projectLog.name.contact_delete"),
    CONTACT_STAGE(XbbRefTypeEnum.CONTACT.getCode(), 60004, "enum.projectLog.name.contact_stage"),
    CONTACT_TEAM_CREATE(XbbRefTypeEnum.CONTACT.getCode(), 60005, "enum.projectLog.name.contact_team_create"),
    CONTACT_TEAM_UPDATE(XbbRefTypeEnum.CONTACT.getCode(), 60006, "enum.projectLog.name.contact_team_update"),
    CONTACT_TEAM_DELETE(XbbRefTypeEnum.CONTACT.getCode(), 60007, "enum.projectLog.name.contact_team_delete"),
    CONTACT_TEAM_HANDOVER(XbbRefTypeEnum.CONTACT.getCode(), 60008, "enum.projectLog.name.contact_team_handover"),
    CONTACT_CREATE(XbbRefTypeEnum.CONTACT.getCode(), 60010, "enum.projectLog.name.contact_create"),
    CONTACT_DISTRIBUTE(XbbRefTypeEnum.CONTACT.getCode(), 60011, "enum.projectLog.name.contact_distribute"),
    CONTACT_SMS(XbbRefTypeEnum.CONTACT.getCode(), 60012, "enum.projectLog.name.sms_send"),
    CONTACT_RESTORE(XbbRefTypeEnum.CONTACT.getCode(), 60013, "enum.projectLog.name.contact_restore"),

    /**
     * 工单
     */
    WORK_ORDER_UPDATE(XbbRefTypeEnum.WORK_ORDER.getCode(), 70001, "enum.projectLog.name.workorder_update"),
    WORK_ORDER_DELETE(XbbRefTypeEnum.WORK_ORDER.getCode(), 70002, "enum.projectLog.name.workorder_delete"),
    WORK_ORDER_CREATE(XbbRefTypeEnum.WORK_ORDER.getCode(), 70003, "enum.projectLog.name.workorder_create"),

    /**
     * 发票
     */
    INVOICE_CREATE(XbbRefTypeEnum.INVOICE.getCode(), 80001, "enum.projectLog.name.invoice_create"),
    INVOICE_DELETE(XbbRefTypeEnum.INVOICE.getCode(), 80002, "enum.projectLog.name.invoice_delete"),

    PAAS_ADD(XbbRefTypeEnum.PAAS.getCode(),41,"enum.projectLog.name.paas_add"),
    PAAS_DELETE(XbbRefTypeEnum.PAAS.getCode(),42,"enum.projectLog.name.paas_delete"),
    PAAS_STAGE_JUMP(XbbRefTypeEnum.PAAS.getCode(),43,"enum.projectLog.name.paas_stage_jump"),

    PUSH_NOTIFY_ADD(XbbRefTypeEnum.PUSH_NOTIFY.getCode(), 41001, "enum.projectLog.name.pushNotify_add"),
    PUSH_NOTIFY_DELETE(XbbRefTypeEnum.PUSH_NOTIFY.getCode(), 41002, "enum.projectLog.name.pushNotify_delete"),

    /**
     * 跟进记录
     */
    CUSTOMER_COMMUNICATE_CREATE(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(), 5001, "enum.projectLog.name.communicate_create"),
    CUSTOMER_COMMUNICATE_DELETE(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(), 5002, "enum.projectLog.name.communicate_delete"),


    /**
     * 访客计划
     */
    CUSTOMER_COMMUNICATE_PLAN_CREATE(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode(), 6001, "enum.projectLog.name.communicatePlan_create"),
    CUSTOMER_COMMUNICATE_PLAN_DELETE(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode(), 6002, "enum.projectLog.name.communicatePlan_delete"),

    /**
     * 竞争对手
     */
    COMPETITOR_CREATE(XbbRefTypeEnum.COMPETITOR.getCode(),202001,"enum.projectLog.name.competitor_create"),
    COMPETITOR_DELETE(XbbRefTypeEnum.COMPETITOR.getCode(),202002,"enum.projectLog.name.competitor_delete"),
    COMPETITOR_UPDATE(XbbRefTypeEnum.COMPETITOR.getCode(),202003,"enum.projectLog.name.competitor_update"),

    UNKNOW(XbbRefTypeEnum.UNKNOWN.getCode(), 404, XbbRefTypeEnum.UNKNOWN.getAlias()),
    ;
    private Integer businessType;
    private Integer subType;
    private String name;

    ProjectLogEnum(Integer businessType, Integer subType, String name) {
        this.businessType = businessType;
        this.subType = subType;
        this.name = name;
    }

    public static ProjectLogEnum getBySubType(Integer subType) {
        for (ProjectLogEnum cacheCode : values()) {
            if (subType.equals(cacheCode.getSubType())) {
                return cacheCode;
            }
        }
        return null;
    }

    /**
     * 根据日志类型获取业务类型
     * @param subType
     * @return
     */
    public static XbbRefTypeEnum getBusinessTypeBySubType(Integer subType) {
        for (ProjectLogEnum value : values()) {
            if (value.getSubType().equals(subType)) {
                return XbbRefTypeEnum.getByCode(value.getBusinessType());
            }
        }
        return XbbRefTypeEnum.UNKNOWN;
    }

    /**
     * 根据日志类型获取业务类型code
     * @param subType
     * @return
     */
    public static Integer getBusinessTypeCodeBySubType(Integer subType) {
        return getBusinessTypeBySubType(subType).getCode();
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }


    private static final Map<OperateTypeEnum, List<ProjectLogEnum>> TEAM_MAP = new HashMap<>();
    static {
        TEAM_MAP.put(OperateTypeEnum.EDIT, Arrays.asList(CUSTOMER_TEAM_UPDATE, CONTRACT_TEAM_UPDATE, CONTACT_TEAM_UPDATE, OPPORTUNITY_TEAM_UPDATE));
        TEAM_MAP.put(OperateTypeEnum.DELETE, Arrays.asList(CUSTOMER_TEAM_DELETE, CONTRACT_TEAM_DELETE, CONTACT_TEAM_DELETE, OPPORTUNITY_TEAM_DELETE));
        TEAM_MAP.put(OperateTypeEnum.NEW, Arrays.asList(CUSTOMER_TEAM_ADD, CONTRACT_TEAM_ADD, CONTACT_TEAM_CREATE, OPPORTUNITY_TEAM_ADD));
        TEAM_MAP.put(OperateTypeEnum.HANDOVER, Arrays.asList(CUSTOMER_TEAM_HANDOVER, CONTRACT_TEAM_HANDOVER, CONTACT_TEAM_HANDOVER, OPPORTUNITY_TEAM_HANDOVER));

    }

    /**
     * 根据业务类型获取团队的subType
     * @param businessType
     * @param operateTypeEnum
     * @return
     */
    public static Integer teamLogSubTypeByBusinessType(Integer businessType, OperateTypeEnum operateTypeEnum) {
        List<ProjectLogEnum> projectLogEnums = TEAM_MAP.get(operateTypeEnum);
        if (Objects.isNull(projectLogEnums) || projectLogEnums.isEmpty()) {
            return UNKNOW.getSubType();
        }
        for (ProjectLogEnum projectLogEnum : projectLogEnums) {
            if (projectLogEnum.getBusinessType().equals(businessType)) {
                return projectLogEnum.getSubType();
            }
        }
        return UNKNOW.getSubType();
    }
}
